<?php
/**
 * @author cuonglv@vietincom.com
 * @since 24/08/2010
 * 
 *
 */

require_once 'Zend/Loader.php';
Zend_Loader::loadClass('Zend_Db');
Zend_Loader::loadClass('Zend_Db_Table');
Zend_Loader::loadClass('Zend_Config_Ini');

class Globals
{
    private static $_db       = null;
    private static $_config   = null;
    
    public function __construct()
    {
        
    }
    
    static public function getDBConnection()
    {
        if (self::$_db != null) {
            return self::$_db;
        } 
        

        self::$_db = Zend_Db::factory('Pdo_Mysql',
                                      array ('host'     => self::getConfig()->resources->db->host,
                                             'username' => self::getConfig()->resources->db->username,
                                             'password' => self::getConfig()->resources->db->password,
                                             'dbname'   => self::getConfig()->resources->db->dbname,
                                             'profiler' => true
                                          ));
        
        self::$_db->query("SET NAMES 'utf8'");

        return self::$_db;
    }
    

    // public static function getConfig()
    public static function getConfig($tag='database')
    {
        if (self::$_config != null) {
            return self::$_config;
        }
        $options['nestSeparator'] = ':';
        self::$_config = new Zend_Config_Ini(
            APPLICATION_PATH.'/configs/application.ini',
            $tag,true);
       return self::$_config;
    } 
	
    public static function getLanguage($controller)
    {
        $langSession = new Zend_Session_Namespace("language");
        if($langSession->type == VN)
        {
            $controller->view->lang_data = new Zend_Translate('csv',ROOT_LANGUAGE.DS.'language_vn.csv');
            $controller->view->langType = 'vn';
        }else{
            $controller->view->lang_data = new Zend_Translate('csv',ROOT_LANGUAGE.DS.'language_en.csv');
            $controller->view->langType = 'en';
        }
    }


    public static function setLanguage($type)
    {
        $langSession = new Zend_Session_Namespace("language");
        if($type == "vn" || is_null($type))
            $langSession->type = VN;
        elseif($type == "en")
            $langSession->type = EN;
    }

    public static function transferObjectToArray($obj)
    {
        $data = array();
        $arrKey = array_keys((array)$obj);
        foreach($arrKey as $key)
        {
            if(!is_object($obj->$key))
                $data[$key] = $obj->$key;
        }

        return $data;
    }

    public static function transferArrayToObject($data, $obj)
    {
        $arrKey = array_keys((array)$obj);
        foreach($arrKey as $key)
        {
            $obj->$key = $data[$key];
        }

        return $obj;
    }

    public static function uploadSingleFile($userId)
    {
        try
        {
            //for upload file
            $adapterImage = new Zend_File_Transfer_Adapter_Http();
            $adapterImage->addValidator('Extension', false, 'jpg,png,jpeg,bmp,gif')
                            ->addValidator('FilesSize',false,'2MB')
                            ->addFilter('Rename',array('target' => ROOT_PUBLIC_UPLOADS.DS.time().'_'.$userId.'.jpg'));
            
            $adapterImage->receive();
            return $adapterImage->isReceived();
        }
        catch(Zend_File_Transfer_Exception $ex)
        {
            throw new Zend_File_Transfer_Exception($ex->getMessage(), $fileerror);
        }
    }

    public static function deleteUploadFileByName($fileName)
    {
        if($fileName)
        {
            chmod(ROOT_PUBLIC_UPLOADS.DS.$fileName, '777');
            return unlink(ROOT_PUBLIC_UPLOADS.DS.$fileName);            
        }
        else
            return true;
    }
}
